package com.example.bookstore;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.w3c.dom.Text;

import java.util.List;

public class BooksAdapter extends RecyclerView.Adapter<BooksAdapter.ViewHolder> {
    private List<Book> bookList ;
    public BooksAdapter(List<Book> bookList) {
        this.bookList = bookList;
    }



    @NonNull
    @Override
    public BooksAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.books,parent,false);
        ViewHolder holder = new ViewHolder(view);
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Book book = bookList.get(position);
//                Toast.makeText(v.getContext(),"跳转到id为"+book.getId(),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(v.getContext(), BookDetailActivity.class);
                intent.putExtra("book", book);
                v.getContext().startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BooksAdapter.ViewHolder holder, int position) {
        Book book = bookList.get(position);
        holder.name.setText(book.getBookName());
        holder.image.setImageURL(book.getBookImage());
        holder.price.setText(book.getBookPrice());
        holder.title.setText(book.getBookTitle());
        holder.press.setText(book.getBookPress());
        holder.author.setText(book.getBookAuthor());
        holder.publish_time.setText(book.getPressDate());
    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        View view;
        MyImageView image;
        private TextView name;
        private TextView price;
        private TextView title;
        private TextView publish_time;
        private TextView author;
        private TextView press;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            image = (MyImageView) itemView.findViewById(R.id.book_image);
            name = (TextView)itemView.findViewById(R.id.book_name);
            price = (TextView)itemView.findViewById(R.id.book_price);
            title =(TextView)itemView.findViewById(R.id.long_title);
            publish_time = (TextView)itemView.findViewById(R.id.book_publish_time);
            author = (TextView)itemView.findViewById(R.id.book_author);
            press = (TextView)itemView.findViewById(R.id.book_press);

        }
    }
}
